package com.plat.mapper;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author carter
 * create time    2018-08-18
 */
public abstract class AbstractParamEntity implements Serializable {

    private static final int Field_Keep_Max_Length = 25;

    @JsonIgnore
    private Integer start = 0;
    @JsonIgnore
    private Integer limit = 1000;
    @JsonIgnore
    private String  orderBy;
    @JsonIgnore
    private boolean order = true;

    @JsonIgnore
    private LocalDateTime minCreateAt;
    @JsonIgnore
    private LocalDateTime maxCreateAt;

    @JsonIgnore
    private List<String> notNullFields ;
    @JsonIgnore
    private boolean useNotNullCondition = false;

    @JsonIgnore
    private List<String> forceIndexName;
    @JsonIgnore
    private boolean forceUseIndex = false;

    @JsonIgnore
    private List<Integer> authorityDeptIds;
    @JsonIgnore
    private List<Integer> wmsList;

    public abstract Integer getId();
    public abstract String  getTable();

    public abstract void setId(Integer start);

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public boolean getOrder() {
        return order;
    }

    public void setOrder(boolean order) {
        this.order = order;
    }

    public LocalDateTime getMinCreateAt() {
        return minCreateAt;
    }

    public void setMinCreateAt(LocalDateTime minCreateAt) {
        this.minCreateAt = minCreateAt;
    }

    public LocalDateTime getMaxCreateAt() {
        return maxCreateAt;
    }

    public void setMaxCreateAt(LocalDateTime maxCreateAt) {
        this.maxCreateAt = maxCreateAt;
    }

    public List<String> getNotNullFields() {
        return notNullFields;
    }

    public void setNotNullFields(List<String> notNullFields) {
        this.notNullFields = notNullFields;
    }

    public boolean isUseNotNullCondition() {
        return useNotNullCondition;
    }

    public void setUseNotNullCondition(boolean useNotNullCondition) {
        this.useNotNullCondition = useNotNullCondition;
    }

    public List<String> getForceIndexName() {
        return forceIndexName;
    }

    public void setForceIndexName(List<String> forceIndexName) {
        this.forceIndexName = forceIndexName;
    }

    public boolean isForceUseIndex() {
        return forceUseIndex;
    }

    public void setForceUseIndex(boolean forceUseIndex) {
        this.forceUseIndex = forceUseIndex;
    }

    public List<Integer> getAuthorityDeptIds() {
        return authorityDeptIds;
    }

    public void setAuthorityDeptIds(List<Integer> authorityDeptIds) {
        this.authorityDeptIds = authorityDeptIds;
    }

    public List<Integer> getWmsList() {
        return wmsList;
    }

    public void setWmsList(List<Integer> wmsList) {
        this.wmsList = wmsList;
    }

    @Override
    public String toString() {

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(
                Arrays.asList(this.getClass().getMethods()).stream()
                        .filter(m -> m.getName().length() > 3)
                        .filter(m -> m.getName().startsWith("get"))
                        .filter(m -> m.getParameterCount() == 0)
                        .map(m->{
                                    String name  = Character.toLowerCase(m.getName().charAt(3))+ m.getName().substring(4);
                                    try {
                                        String value = Optional.ofNullable(m.invoke(this)).orElse("Null").toString();
                                        if (value.length()> Field_Keep_Max_Length)
                                        {
                                            value = value.substring(0, Field_Keep_Max_Length)+"...";
                                        }
                                        return name.concat("=").concat(value);
                                    } catch (Exception e) {
                                        return name.concat("=").concat("<Exception:"+e.getMessage()+">");
                                    }
                        })
                        .collect(Collectors.joining(",",this.getClass().getSimpleName()+"【","】"))
        );


        return stringBuffer.toString();

    }
}
